package cn.qdhlm.frontendprocessor.common.util;



import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Name: ComUtil
 * @Description: ComUtil
 * @Author: jiangcs
 **/
public class ComUtil {
    public static boolean isEmpty(Object object) {
        return StringUtils.isEmpty(object);
    }

    /**
     * isZero
     *
     * @param object
     * @return
     */
    public static boolean isZero(Object object) {
        return safeInt(object) == 0;
    }

    /**
     * is contain
     *
     * @param source
     * @param key
     * @return
     */
    public static boolean isContain(Object source, Object key) {
        if (!ComUtil.isEmpty(source) && !ComUtil.isEmpty(key)) {
            if (source.getClass().isArray()) {
                Object[] sourceArr = (Object[]) source;
                for (int i = 0; i < sourceArr.length; i++) {
                    if (key.equals(sourceArr[i])) {
                        return true;
                    }
                }
            } else {
                return ("," + source + ",").contains("," + key + ",");
            }
        }
        return false;
    }

    /**
     * safeString
     *
     * @param object
     * @return
     */
    public static String safeString(Object object) {
        if (isEmpty(object)) {
            return "";
        }
        return String.valueOf(object);
    }

    /**
     * safeInt
     *
     * @param object
     * @return
     */
    public static int safeInt(Object object) {
        if (isEmpty(object)) {
            return 0;
        }
        try {
            if (object instanceof Double) {
                return ((Double) object).intValue();
            }
            if (object instanceof Float) {
                return ((Float) object).intValue();
            }
            return Integer.parseInt(safeString(object));
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * Object 2 Double
     *
     * @param object
     * @return
     */
    public static double safeDouble(Object object) {
        if (isEmpty(object)) {
            return 0;
        }
        try {
            return Double.parseDouble(safeString(object));
        } catch (Exception e) {
            return 0D;
        }
    }

    /**
     * 格式化日期时间
     *
     * @param pattern
     * @param date
     * @return
     */
    public static String formatDateTime(Date date, String pattern) {
        if (isEmpty(date)) {
            return "";
        }
        if (isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 根据length 左补零
     *
     * @param object
     * @param length
     * @return
     */
    public static String fillZeroLeft(Object object, int length) {
        StringBuilder sbStr = new StringBuilder(safeString(object));
        int lengthDiff = length - sbStr.length();
        if (lengthDiff > 0) {
            for (int i = 0; i < lengthDiff; i++) {
                sbStr.insert(0, "0");
            }
        }
        return sbStr.toString();
    }

    /**
     * 格式化 Double类型 小数
     *
     * @param d
     * @param scale
     * @return
     */
    public static Double formatScale(Double d, int scale) {
        BigDecimal bigDecimal = BigDecimal.valueOf(d).setScale(scale, RoundingMode.HALF_UP);
        return bigDecimal.doubleValue();
    }

    /**
     * int 2 double
     *
     * @param i
     * @param d
     * @param point
     * @return
     */
    public static double doubleConvert(int i, Double d, int point) {
        return formatScale(i * Math.pow(d, point), point);
    }

    /**
     * 单位 转换
     *
     * @param d
     * @param unit
     * @param protocolCode
     * @return
     */
    public static double unitConvert(double d, int unit, String protocolCode) {
        double num = 1;
        // 京源热表(NB) || 京源IC卡阀控热表(NB) || 京源大口径热表(NB)
        if ("CJT188_0033".equals(protocolCode) || "CJT188_0053".equals(protocolCode) || "CJT188_0060".equals(protocolCode)) {
            // 转换KWH
            if (unit > 2 && unit <= 10) {
                num *= Math.pow(10, unit - 2) * 0.001;
            }
            // j转换成kw
            else if (unit >= 11 && unit <= 17) {
                num *= Math.pow(10, unit - 11) * 0.001 / 3.6;
            }
            // 转换成kw
            else if (unit >= 19 && unit <= 28) {
                num *= Math.pow(10, unit - 19) * 0.0001;
            }
            // 转成M3/H
            else if (unit == 32) {
                num *= 0.001;
            }
            // 转成M3
            else if (unit >= 39 && unit <= 44) {
                num *= Math.pow(10, unit - 39) * 0.00001;
            }
            // 转成M3/H
            else if (unit >= 46 && unit <= 50) {
                num *= Math.pow(10, unit - 46) * 0.0001;
            }
        }
        // 兰吉尔UH50热表 || 兰吉尔f4热表
        else if ("EN1434_0001".equals(protocolCode) || "EN1434_0003".equals((protocolCode))) {
            // kwh
            if (unit > 2 && unit <= 9) {
                num *= Math.pow(10, unit - 6);
            }
            // GJ
            else if (unit > 13 && unit <= 19) {
                num *= Math.pow(10, unit - 17);
            }
            // m3
            else if (unit > 19 && unit <= 25) {
                num *= Math.pow(10, unit - 22);
            }
            // h 默认不操作
            // kw
            else if (unit > 42 && unit <= 46) {
                num *= Math.pow(10, unit - 46);
            }
            // m3/h
            else if (unit > 58 && unit <= 64) {
                num *= Math.pow(10, unit - 62);
            }

            // 0.1 ℃
            else if (unit == 98) {
                num *= 0.1;
            }
        }
        // 国标
        else {
            // 转换KWH
            if (unit > 2 && unit <= 10) {
                num *= Math.pow(10, unit - 2) * 0.001;
            }
            // j转换成kw
            else if (unit >= 11 && unit <= 19) {
                num *= Math.pow(10, unit - 11) * 0.001 / 3.6;
            }
            // 转换成kw
            else if (unit >= 20 && unit <= 28) {
                num *= Math.pow(10, unit - 20) * 0.001;
            }
            // 转成M3
            else if (unit >= 41 && unit <= 46) {
                num *= Math.pow(10, unit - 41) * 0.001;
            }
            // 转成M3/H
            else if (unit >= 50 && unit <= 55) {
                num *= Math.pow(10, unit - 50) * 0.001;
            }
        }
        return num * d;
    }

    /**
     * 跳过68之前的FE
     *
     * @param byteBuf
     * @return
     */
/*    public static int skipFE(ByteBuf byteBuf) {
        int skipByteCount = 0;
        for (int i = 0, len = byteBuf.readableBytes(); i < len; i++) {
            String hexStr = CodeUtil.byteToHexString(byteBuf.getByte(i));
            // 68：结束执行
            if (Constant.SYMBOL_68.equalsIgnoreCase(hexStr)) {
                break;
            }

            // FE：跳过
            if (Constant.SYMBOL_FE.equalsIgnoreCase(hexStr)) {
                byteBuf.skipBytes(1);

                skipByteCount++;
            }
        }
        return skipByteCount;
    }*/

    /**
     * CRC校验和: modbus
     *
     * @param bytes
     * @return
     */
    public static int crc4Modbus(byte[] bytes) {
        int crc = 0x0000ffff;
        for (byte b : bytes) {
            crc ^= ((int) b & 0x000000ff);
            for (int j = 0; j < 8; j++) {
                if ((crc & 0x00000001) != 0) {
                    crc >>= 1;
                    crc ^= 0x0000a001;
                } else {
                    crc >>= 1;
                }
            }
        }
        return ((crc & 0xff00) >> 8) | ((crc & 0x00ff) << 8);
    }
}
